{
 "cells": [
  {
   "cell_type": "markdown",
   "id": "3a79ef54-63e6-429f-ba9f-590b736a7f29",
   "metadata": {},
   "source": [
    "## 2.11 增加张量的维度"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "c90a18b9-d8e8-431c-856c-35f1e4ed05e1",
   "metadata": {},
   "source": [
    "### 1.任务描述\n",
    "- 创建值为[0 1 2 3 4]的一维张量re1\n",
    "- 在re1的0轴上增加一个维度，得到re2\n",
    "- 在re1的1轴上增加一个维度，得到re3\n",
    "- 在re1的最后一个轴上增加一个维度，得到re4\n",
    "- 创建值为[[1,2],[3,4]]的二维张量re5\n",
    "- 在re5的0轴上增加一个维度，得到re6\n",
    "- 在re5的1轴上增加一个维度，得到re7\n",
    "- 在re5的2轴上增加一个维度，得到re8\n",
    "- 在re5的最后一个轴上增加一个维度，得到re9"
   ]
  },
  {
   "attachments": {},
   "cell_type": "markdown",
   "id": "8d7baa9c-93a2-42f3-a3c1-231cdb587f2d",
   "metadata": {},
   "source": [
    "### 2.知识准备\n",
    "\n",
    "见教程。"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "74ad989a-9b82-43e1-b841-e74284cd5936",
   "metadata": {},
   "source": [
    "### 3.任务分析\n",
    "\n",
    "可以使用tf.expand_dims方法对张量增加维度"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "435c6090-cfda-4f46-a550-22a368e41e4a",
   "metadata": {},
   "source": [
    "### 4.任务实施\n",
    "\n"
   ]
  },
  {
   "attachments": {},
   "cell_type": "markdown",
   "id": "ec75eb6c-5da3-467d-a471-ca3b47242dd6",
   "metadata": {},
   "source": [
    "执行代码"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "id": "2ae9da58-e339-4d22-9f8d-ca255711d89e",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "tf.Tensor([0 1 2 3 4], shape=(5,), dtype=int32)\n",
      "tf.Tensor([[0 1 2 3 4]], shape=(1, 5), dtype=int32)\n",
      "tf.Tensor(\n",
      "[[0]\n",
      " [1]\n",
      " [2]\n",
      " [3]\n",
      " [4]], shape=(5, 1), dtype=int32)\n",
      "tf.Tensor(\n",
      "[[0]\n",
      " [1]\n",
      " [2]\n",
      " [3]\n",
      " [4]], shape=(5, 1), dtype=int32)\n",
      "tf.Tensor(\n",
      "[[1 2]\n",
      " [3 4]], shape=(2, 2), dtype=int32)\n",
      "tf.Tensor(\n",
      "[[[1 2]\n",
      "  [3 4]]], shape=(1, 2, 2), dtype=int32)\n",
      "tf.Tensor(\n",
      "[[[1 2]]\n",
      "\n",
      " [[3 4]]], shape=(2, 1, 2), dtype=int32)\n",
      "tf.Tensor(\n",
      "[[[1]\n",
      "  [2]]\n",
      "\n",
      " [[3]\n",
      "  [4]]], shape=(2, 2, 1), dtype=int32)\n",
      "tf.Tensor(\n",
      "[[[1]\n",
      "  [2]]\n",
      "\n",
      " [[3]\n",
      "  [4]]], shape=(2, 2, 1), dtype=int32)\n"
     ]
    }
   ],
   "source": [
    "import tensorflow as tf\n",
    "import numpy as np\n",
    "# 创建值为[0 1 2 3 4]的一维张量re1\n",
    "re1=tf.range(5)\n",
    "print(re1)\n",
    "# 在re1的0轴上增加一个维度，得到re2\n",
    "re2=tf.expand_dims(re1,0)\n",
    "print(re2)\n",
    "# 在re1的1轴上增加一个维度，得到re3\n",
    "re3=tf.expand_dims(re1,1)\n",
    "print(re3)\n",
    "# 在re1的最后一个轴上增加一个维度，得到re4\n",
    "re4 = tf.expand_dims(re1,-1) \n",
    "print(re4)\n",
    "\n",
    "# 创建值为[[1,2],[3,4]]的二维张量re5\n",
    "re5=tf.constant([[1,2],[3,4]])\n",
    "print(re5)\n",
    "# 在re5的0轴上增加一个维度，得到re6\n",
    "re6=tf.expand_dims(re5,0)\n",
    "print(re6)\n",
    "# 在re5的1轴上增加一个维度，得到re7\n",
    "re7=tf.expand_dims(re5,1)\n",
    "print(re7)\n",
    "# 在re5的2轴上增加一个维度，得到re8\n",
    "re8=tf.expand_dims(re5,2)\n",
    "print(re8)\n",
    "# 在re5的最后一个轴上增加一个维度，得到re9\n",
    "re9 = tf.expand_dims(re5,-1) \n",
    "print(re9)"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3 (ipykernel)",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.9.10"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
